﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Datastore.Common;

namespace Widgetsphere.Autos.API.FacetedNavigation
{
    public static class RepositoryHelper
    {
        private const int CACHE_TIME_LIMIT = 5;
        private static UserCredentials _credentials;
        public const string REPOSITORYID = "d55cd480-abe9-4c83-8d24-52ed38588bbc";

        public static ListingResults Query(ListingQuery query)
        {
            try
            {
                using (var factory = RepositoryConnection.GetFactory(ConfigHelper.DatastoreServer, ConfigHelper.DatastorePort))
                {
                    var service = factory.CreateChannel();
                    query.Credentials = Credentials;
                    var connection = new RepositoryConnection(new Guid(REPOSITORYID));
                    return connection.QueryData(query, service);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        // this is a cache set of non-filtered results
        public static ListingResults MasterResults
        {
            get
            {
                using (var factory = RepositoryConnection.GetFactory(ConfigHelper.DatastoreServer, ConfigHelper.DatastorePort))
                {
                    var service = factory.CreateChannel();
                    var connection = new RepositoryConnection(new Guid(REPOSITORYID));
                    return connection.QueryData(new ListingQuery() { Credentials = Credentials }, service);
                }
            }
        }

        public static string GetFormattedName(string name)
        {
            switch (name.ToLower())
            {
                case "interiorcolor":
                    return "Interior Color";
                case "exteriorcolor":
                    return "Exterior Color";
                default:
                    return name;
            }
        }

        private static object _locker = new object();
        public static UserCredentials Credentials
        {
            get
            {
                lock (_locker)
                {
                    if (_credentials == null)
                    {
                        using (var factory = SystemCoreInteractDomain.GetCoreFactory(ConfigHelper.DatastoreServer, ConfigHelper.DatastorePort))
                        {
                            var server = factory.CreateChannel();
                            string publicKey;
                            try
                            {
                                publicKey = server.GetPublicKey();
                            }
                            catch (Exception ex)
                            {
                                return null;
                            }

                            _credentials = new UserCredentials();
                            _credentials.UserName = "root";
                            _credentials.Password = "password";
                            _credentials.Password = SecurityHelper.Encrypt(publicKey, _credentials.Password);
                        }
                    }
                }
                return _credentials;
            }
        }

    }
}